import 'package:flutter_base_app/config/i18n/i18n.dart';
import 'package:flutter_base_app/config/i18n/i18n_key.dart';
import 'package:flutter_base_app/page/view/home_page.dart';
import 'package:flutter_base_app/page/vm/main_vm.dart';
import 'package:flutter_base_app/utils/app_common_live.dart';
import 'package:flutter_base_app/widgets/image/svg_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_framework/base_framework_export.dart';
import 'package:get/get.dart';

import '../../config/app_colors.dart';
import '../../generated/assets.dart';
import '../../widgets/base/menu_widget.dart';

enum MainPageType {
  HOME(0),
  HOME2(1),
  HOME3(2),
  ME(3);

  final int id;

  const MainPageType(this.id);
}

/// 主面板
class MainPage extends BasePage {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class MainPageIndex extends MenuItemData {
  final Widget widget;
  final MainPageType type;

  MainPageIndex(super.index, super.labelId, super.icon, this.type, this.widget);
}

class _MainPageState extends BaseMvvmState<MainPage, MainVm> {
  _MainPageState() : super(MainVm());
  final currentTab = MainPageType.HOME.obs;
  final List<MainPageIndex> _pages = [
    MainPageIndex(0, I18nKey.home, Assets.svgIcGeneral, MainPageType.HOME, const HomePage()),
    MainPageIndex(1, I18nKey.home, Assets.svgIcGeneral, MainPageType.HOME2, const HomePage()),
    MainPageIndex(2, I18nKey.home, Assets.svgIcGeneral, MainPageType.HOME3, const HomePage()),
    MainPageIndex(3, I18nKey.home, Assets.svgIcGeneral, MainPageType.ME, const HomePage()),
  ];
  final _pageController = PageController(initialPage: 0);

  final TextStyle _selectedLabelStyle = const TextStyle(
    fontSize: 11.0,
    fontWeight: FontWeight.w500,
  );
  final TextStyle _unselectedLabelStyle = const TextStyle(
    fontSize: 11.0,
    fontWeight: FontWeight.w400,
  );

  @override
  void initState() {
    super.initState();
    AppLiveData.homeTab.post(currentTab.value);
    //监听app生命周期
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  PreferredSizeWidget? createAppbar() {
    return null;
  }

  @override
  Widget buildWidget(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: PageView(
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            currentTab.value = _pages[index].type;
          },
          children: _pages.map((it) => it.widget).toList(),
        ),
        bottomNavigationBar: Container(
          decoration: BoxDecoration(
            border: Border(
              top: BorderSide(
                width: 0.5,
                color: ColorsApp.gray_e5,
              ),
            ),
          ),
          child: Obx(() => BottomNavigationBar(
                selectedItemColor: ColorsApp.primaryColor,
                unselectedItemColor: ColorsApp.black_26,
                showUnselectedLabels: true,
                type: BottomNavigationBarType.fixed,
                elevation: 0.0,
                selectedLabelStyle: _selectedLabelStyle,
                unselectedLabelStyle: _unselectedLabelStyle,
                backgroundColor: Theme.of(context).cardColor,
                currentIndex: currentTab.value.index,
                items: _pages
                    .map((it) => BottomNavigationBarItem(
                          icon: SvgWidget(
                            it.icon,
                            width: 24,
                            height: 24,
                            color: ColorsApp.black_26,
                          ),
                          activeIcon: SvgWidget(it.icon, width: 24, height: 24, color: ColorsApp.primaryColor),
                          label: it.labelId.trx,
                          tooltip: it.labelId.trx,
                        ))
                    .toList(),
                onTap: (int index) async {
                  _handlePageChanged(index);
                  // if (!AppGlabol.isLogin && index == 3) {
                  //   AppRouterJump.login();
                  // } else {
                  //   _handlePageChanged(index);
                  // }
                },
              )),
        ));
  }

  NavigationRailDestination createMenuItem(String title, String icon) {
    return NavigationRailDestination(
      icon: Builder(builder: (context) {
        return SvgWidget(icon, width: 24, height: 24);
      }),
      label: Text(title),
    );
  }

  void _handlePageChanged(int index) {
    _pageController.jumpToPage(index);
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }
}
